package com.fz.us.admin.base.bean.search.projection;

import com.fz.us.admin.base.bean.search.SearchEnum;
import com.fz.us.admin.base.utils.CommonUtil;
import org.springframework.util.Assert;

/**
 * 查询 - 参数 - ProjectionAggregate
 * ============================================================================
 * 版权所有 2016 。
 *
 * @author fallenpanda
 * @version 1.0 2016-07-13 。
 * ============================================================================
 */
public class ProjectionAggregate extends Projection {

    private final String field;

    private ProjectionAggregate(SearchEnum.ProjectionType projectionType, String field) {
        super(projectionType);
        this.field = field;
    }

    public String getField() {
        return field;
    }

    public static ProjectionAggregate max(String field) {
        return newBuilder().setProjectionType(SearchEnum.ProjectionType.max).setField(field).build();
    }

    public static ProjectionAggregate min(String field) {
        return newBuilder().setProjectionType(SearchEnum.ProjectionType.min).setField(field).build();
    }

    public static ProjectionAggregate avg(String field) {
        return newBuilder().setProjectionType(SearchEnum.ProjectionType.avg).setField(field).build();
    }

    public static ProjectionAggregate sum(String field) {
        return newBuilder().setProjectionType(SearchEnum.ProjectionType.sum).setField(field).build();
    }

    public static Builder newBuilder() {
        return new Builder();
    }

    public static class Builder extends Projection.Builder<Projection, Builder> {

        private String field;

        @Override
        protected Builder getThis() {
            return this;
        }

        public Builder setField(String field) {
            this.field = field;
            return this;
        }

        @Override
        public ProjectionAggregate build() {
            Assert.notNull(projectionType, "projectionType 不能为空");
            Assert.hasText(field, "field 不能为空");
            Assert.isTrue(CommonUtil.isField(field), "field 非法参数");
            return new ProjectionAggregate(projectionType, field);
        }

    }

}
